<%--
    Document   : modificacionRouter
    Author     : José Manuel
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="java.util.*"%>
<%@page import="proyectoCisco.Router"%>
<%@page import="proyectoCisco.claseBBDD"%>

<html>
    <head>
        <link href="estilos.css" rel="stylesheet" type="text/css">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Modificacion de router</title>
        <SCRIPT LANGUAGE="JavaScript1.2">
            //Determine if a string is a valid IP address
        function isIP(testStr)
        {
          var     dots = 0;
          for( var i = 0 ; i < testStr.length ; ++i )
          {
            if( testStr.charAt(i) >= '0' && testStr.charAt(i) <= '9') {
               continue;
            } else if( testStr.charAt(i) == '.' ) {
               ++dots;
               continue;
            } else {
               break;
            }
          }
          if (i==0 || i!=testStr.length || dots != 3)
            return false;

          var fieldArray = testStr.split(".")
          var octet1 = parseInt(fieldArray[0], 10)
          var octet2 = parseInt(fieldArray[1], 10)
          var octet3 = parseInt(fieldArray[2], 10)
          var octet4 = parseInt(fieldArray[3], 10)
          //make sure no blank fields
          if (isNaN(octet1) || isNaN(octet2) || isNaN(octet3) || isNaN(octet4))
            return false
          if (octet1 < 0 || octet1 > 255)
            return false;
          if (octet2 < 0 || octet2 > 255)
            return false;
          if (octet3 < 0 || octet3 > 255)
            return false;
          if (octet4 < 0 || octet4 > 255)
            return false;
          return true;
        }

        // Check whether the ip address is valid
        function isValidHostIP(ipStr, netMask)
        {
                if((isIP(ipStr) != true) || isIP(netMask) != true)
                        return false;

                var fieldArray = ipStr.split(".");
                var maskArray = netMask.split(".");
                var octet = new Array();
                var mask = new Array();
                var network = 0;
                var hostID = 0;
                for(i = 0; i < 4; i++) {
                        octet[i] = parseInt(fieldArray[i], 10);
                        mask[i] = parseInt(maskArray[i], 10);
                }

                if(octet[0] < 1 || octet[0] > 223) {
                        // the IP address is not a class A or B or C address
                        return false;
                }

                if(mask[0] < 128 || octet[3] > 254) {
                        // the subnet mask is invalid
                        return false;
                }

                // And the ip and mask to get the network.
                // xor the ip and network address to get the host.
                // if the sum of any host part octet and corrsponding
                // mask is not <mask> or 255, it is valid
                var valid = false;
                for(i = 0; i < 4; i++) {
                        network = octet[i] & mask[i];
                        hostID = octet[i] ^ network;
                        if(mask[i] != 255) {
                                hostID += mask[i];
                                if(hostID != mask[i] && hostID != 255)
                                        valid = true;
                        }
                }
                return valid;
        }

            // Funcion de envio de datos
            function valida_envia(){
                if(document.router.ip.value=="" || document.router.mascara.value==""
                   || document.router.nombre.value=="" || document.router.sistemaOperativo.value=="")
                {
                        alert('rellene todos los campos');
                        return 0
                }
                if(isValidHostIP(document.router.ip.value,document.router.mascara.value)==false)
                {
                        alert('La ip no es correcta!');
                        return 0
                }
                // Hacemos el submit
                document.router.submit();
                }
        </SCRIPT>
    </head>
    <body>
     <%
        String ip;
        Router router = new Router();
        claseBBDD baseDatos = new claseBBDD();

        ip=request.getParameter("ip");
        // Comprobamos si existe usuario en la sesion
        if (session.getAttribute("login") != null && ((String) session.getAttribute("login")).length() > 0)
        {
             router=baseDatos.ObtenerDatosRouter(ip);
                    %>
                    <form action="nuevaModificacionRouter.jsp" method="get" name="router">
                        <fieldset>
                            <legend>Modificar datos router</legend>
                            
                                <table width="400">
                                    <caption>Datos del router '<%= router.getIp() %>'</caption>
                                        <tbody>
                                            <tr>
                                                <td> Ip: </td>
                                                <td> <input type="text" name="ip" size="40" value="<%=router.getIp()%>" readonly> </td>
                                            </tr>
                                            <tr>
                                                <td> Máscara: </td>
                                                <td> <input type="text" name="mascara" size="40" value="<%=router.getMascara()%>"> </td>
                                            </tr>
                                            <tr>
                                                <td> Nombre: </td>
                                                <td> <input type="text" name="nombre" size="40" value="<%=router.getNombre()%>"> </td>
                                            </tr>
                                            <tr>
                                                <td> Sistema Operativo: </td>
                                                <td> <input type="text" name="sistemaOperativo" size="40" value="<%=router.getSistemaOperativo()%>"> </td>
                                            </tr>
                                            <%
                                                baseDatos.cerrarConexion();
                                            %>
                                            <tr>
                                                <td colspan="2" align="center">
                                                    <input class="btn" type="button" value="Modificacion nueva" onclick="valida_envia()"></input>
                                                    <input type="reset"></input>
                                                </td>
                                            </tr>
                                        </tbody>
                                    </table>
                        </fieldset>
                  </form>
                  <%
        }
        else
        {
        %><META HTTP-EQUIV="REFRESH" CONTENT="1;URL=error.jsp">
        <%
        }
        %>
     </body>
</html>
